package com.cheyun.netsalev3.view

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.databinding.ActivityAdvancedSearchBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.AdvancedSearchActivityViewModel
import com.cheyun.netsalev3.widget.FlowLayoutManager

class AdvancedSearchActivity : BaseActivity<ActivityAdvancedSearchBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_advanced_search
    }

    override fun initViewModel(): ViewModel? {
        var nkey = ""
        intent.getStringExtra("nkey")?.apply {
            nkey = this
        }
        var factory = InjectorUtils.getAdvancedSearchActivityViewModelFactory(nkey)
        return ViewModelProviders.of(this, factory).get(AdvancedSearchActivityViewModel::class.java)
    }

    override fun initData() {
    }


    private lateinit var adapter: MyAdapter

    override fun initView() {
        binding.recyclerView.layoutManager = FlowLayoutManager()
        adapter = MyAdapter(R.layout.advanced_search_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
        subscribeUi()

        binding.viewModel?.typeShow1?.observe(this, Observer {
            binding.typeshow1.setText(it.key)
            if(it.type == 1)
                binding.typeshow2.visibility = View.VISIBLE
            else
                binding.typeshow2.visibility = View.GONE

            if(it.type == 2)
                binding.editshow1.visibility = View.VISIBLE
            else
                binding.editshow1.visibility = View.GONE
        })

        binding.viewModel?.typeShow2?.observe(this, Observer {
            binding.typeshow2.setText(it.name)
            binding.editshow1.setText(it.name)
        })
    }

    private fun subscribeUi() {
        binding.viewModel?.listItem?.observe(this, Observer { lists ->
            Log.e("subscribeUi", lists.toString())
            if (lists != null) {
                adapter.submitList(lists)
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 102) {
            if(requestCode == 101 || requestCode == 103 || requestCode == 104){
                data?.apply {
                    var carBrandParam = getSerializableExtra("car") as CarBrandParam
                    binding.viewModel?.setCarData(carBrandParam)
                }
            }
            if(requestCode == 102){
                data?.apply {
                    var carBrandParam = getSerializableExtra("car") as CarBrandParam
                    binding.viewModel?.setCarData2(carBrandParam)
                }
            }
        }
    }
}
